package com.jianghu;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import com.jianghu.helpers.GetLineFromtxt;
import com.jianghu.helpers.TrendsShow;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.TextView;

public class Comment extends Activity {
	public Context context = Comment.this;
	public Boolean surplus = true;
	LinearLayout loadingLayout = null;
	LinearLayout commentLayout;
	ProgressBar progressBar;
	private ListView listView;
	DownloadMore downloadMore = null;
	ProgressBar listviewBar;
	List<TrendsShow> dataArray = null;
	private PopupWindow pop;
	Timer timer;
	private LayoutParams mLayoutParams = new LinearLayout.LayoutParams(
			LinearLayout.LayoutParams.WRAP_CONTENT,
			LinearLayout.LayoutParams.WRAP_CONTENT);
	private LayoutParams FFlayoutParams = new LinearLayout.LayoutParams(
			LinearLayout.LayoutParams.FILL_PARENT,
			LinearLayout.LayoutParams.FILL_PARENT);
	ImageAndTextListAdapter listItemAdapter = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.comment);
		
		View view=getLayoutInflater().inflate(R.layout.popwindow, null);
		pop=new PopupWindow(view,LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		commentLayout=(LinearLayout)findViewById(R.id.comment_layout);
		
		listviewBar = (ProgressBar) findViewById(R.id.comm_progressbar);
		LinearLayout layout = new LinearLayout(this);
		layout.setOrientation(LinearLayout.HORIZONTAL);
		progressBar = new ProgressBar(this);
		progressBar.setPadding(0, 0, 15, 0);
		layout.addView(progressBar, mLayoutParams);
		TextView textView = new TextView(this);
		textView.setText("加载中...");
		textView.setGravity(Gravity.CENTER_VERTICAL);
		layout.addView(textView, FFlayoutParams);
		layout.setGravity(Gravity.CENTER);
		loadingLayout = new LinearLayout(this);
		loadingLayout.addView(layout, mLayoutParams);
		loadingLayout.setGravity(Gravity.CENTER);

		listView = (ListView) findViewById(R.id.listViewOfComment);
		dataArray = new ArrayList<TrendsShow>();
		listView.addFooterView(loadingLayout);
		listView.setCacheColorHint(0);
		downloadMore = new DownloadMore();
		downloadMore.execute((Void) null);
		commentLayout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				pop.showAtLocation(commentLayout, Gravity.CENTER, 0, 2000);
				timer=new Timer();
				timer.schedule(new TimerTask() {	
					@Override
					public void run() {
						pop.dismiss();
					}
				},10000);
			}
		});
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (timer!=null) {
				timer.cancel();
			}
			if (pop != null && pop.isShowing()) {
				pop.dismiss();
			}
		}
		return super.onKeyDown(keyCode, event);
	}
	private void loadRemnantListItem(int totalItemCount) {
		int max = totalItemCount + 10;
		for (int i = totalItemCount; i < max; i++) {
			String str = GetLineFromtxt.readAppointedLineNumber(Comment.this,
					i + 1);
			if (str == null) {
				surplus = false;
				break;
			}
			String imageUrl = "http://t3.baidu.com/it/u=3744283480,376574923&fm=0&gp=0.jpg";
			String username = "冷笑话精选";
			String time = i + "小时前";
			String contents = str;
			TrendsShow trendsShow = new TrendsShow(imageUrl, username, time,
					contents);
			dataArray.add(trendsShow);
		}

	}

	class DownloadMore extends AsyncTask<Void, Void, Void> {
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			listviewBar.setVisibility(View.VISIBLE);
		}

		@Override
		protected Void doInBackground(Void... arg0) {
			loadRemnantListItem(0);
			// Log.v("dataarraty", dataArray.size() + " " + surplus);
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// listItemAdapter.notifyDataSetChanged();
			listItemAdapter = new ImageAndTextListAdapter(Comment.this,
					dataArray, listView);
			listView.setAdapter(listItemAdapter);
			listviewBar.setVisibility(View.INVISIBLE);
			if (surplus == false) {
				listView.removeFooterView(loadingLayout);
			}
			super.onPostExecute(result);
		}
	}

	public class ImageAndTextListAdapter extends ArrayAdapter<TrendsShow> {

		public class Holder {
			TextView username, content, time;
			ImageView userImage;
			Button replay;
		}

		// private ListView listView;

		// private AsyncImageLoader asyncImageLoader;

		public ImageAndTextListAdapter(Activity activity,
				List<TrendsShow> TrendsShows, ListView listView) {
			super(activity, 0, TrendsShows);
			// this.listView = listView;
			// asyncImageLoader = new AsyncImageLoader();
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			Activity activity = (Activity) getContext();

			// Inflate the views from XML
			View rowView = convertView;
			Holder holder;
			// ViewCache viewCache;
			if (rowView == null) {
				LayoutInflater inflater = activity.getLayoutInflater();
				rowView = inflater.inflate(R.layout.listview_comment_trends,
						null);
				holder = new Holder();
				holder.username = (TextView) rowView
						.findViewById(R.id.comm_user);
				holder.content = (TextView) rowView
						.findViewById(R.id.comm_content);
				holder.time = (TextView) rowView.findViewById(R.id.comm_replaytime);
				holder.userImage = (ImageView) rowView
						.findViewById(R.id.comm_usericon);
				holder.replay = (Button) rowView.findViewById(R.id.comm_repaly);
				rowView.setTag(holder);
			} else {
				holder=(Holder) convertView.getTag();
			}

			TrendsShow trendsShow = dataArray.get(position);
			if (trendsShow != null) {
				holder.username.setText(trendsShow.getusernameText());
				holder.content.setText(trendsShow.getcontentsText());
				//holder.time.setText("ererer");

				// Load the image and set it on the ImageView
				// String imageUrl = TrendsShow.getImageUrl();
				// ImageView imageView = viewCache.getImageView();
				// imageView.setTag(imageUrl);
				// Drawable cachedImage =
				// asyncImageLoader.loadDrawable(imageUrl,
				// new ImageCallback() {
				// public void imageLoaded(Drawable imageDrawable,
				// String imageUrl) {
				// ImageView imageViewByTag = (ImageView) listView
				// .findViewWithTag(imageUrl);
				// if (imageViewByTag != null) {
				// imageViewByTag
				// .setImageDrawable(imageDrawable);
				// }
				// }
				// });
				// if (cachedImage == null) {
				// imageView.setImageResource(R.drawable.usericon);
				// } else {
				// imageView.setImageResource(R.drawable.usericon);
				// // imageView.setImageDrawable(cachedImage);
				// }
				// imageView.setOnClickListener(new View.OnClickListener() {
				// @Override
				// public void onClick(View arg0) {
				// Intent intent = new Intent();
				// intent.setClass(context, UserTrendsManual.class);
				// Bundle bundle = new Bundle();
				// bundle.putString("item", TrendsShow.getusernameText());
				// bundle.putString("type", "friends");
				// intent.putExtras(bundle);
				// context.startActivity(intent);
				// }
				// });
				// }
				// Set the text on the TextView
				// TextView textTimeView = viewCache.getTimesView();
				// textTimeView.setText(TrendsShow.gettimesText());
				// TextView textUserNameView = viewCache.getUserNameView();
				// textUserNameView.setText(TrendsShow.getusernameText());
				// TextView textCommentsView = viewCache.getCommentsView();
				// textCommentsView.setText(TrendsShow.getcontentsText());

			}
			return rowView;
		}
	}
}
